# -*- coding: utf-8 -*-
# @File:     utils.py
# @Author:
# @DateTime: 2025/06/17/16:45
import io
import os
import sys
import traceback

from .minio.save_minio_tool import save_file_to_stroage

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)

from .tool import *


def save_result_byte_file(file_byte, file_type, base_path):
    folder_path = os.path.join(base_path, "result")
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    save_path = os.path.join(folder_path, f"{get_time_string()}.{file_type}")
    save_byte_file(file_byte, save_path)


def save_result_list(result_list, base_path):
    try:
        for result in result_list:
            file_byte = result['data']
            file_type = result['type']
            save_result_byte_file(file_byte, file_type, base_path)
    except:
        print('保存文件到本地失败')


def save_result_list_to_server(result_list):
    li = []
    try:
        for result in result_list:
            file_byte = result['data']
            file_type = result['type']

            file_io = io.BytesIO(file_byte)
            rel_path = save_file_to_stroage(file_io, file_type=file_type, forlder="generate/result")

            li.append({
                'path': rel_path,
                'type': file_type
            })
    except:
        print(traceback.format_exc())
    return li


